package com.br.mercarad;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import jsyntaxpane.DefaultSyntaxKit;

import com.br.mercarad.db.DbManagerOracle;
import com.br.mercarad.db.DbManagerSqLite;
import com.br.mercarad.db.vos.CampoVO;
import com.br.mercarad.db.vos.ViewVO;
import com.br.mercarad.editor.EditorManager;
import com.br.mercarad.projeto.Projeto;
import com.br.mercarad.projeto.ProjetoVO;
import com.br.mercarad.uteis.Utils;

public class Principal {

	private DbManagerSqLite managerLite;
	private DbManagerOracle managerOracle;

	private JFrame frmMercajava;
	private DefaultMutableTreeNode rootNodeTabelas;
	private DefaultMutableTreeNode rootNodeProjeto;
	private DefaultMutableTreeNode rootNodeViews;
	private DefaultMutableTreeNode rootNodeCamposView;
	private JTree treeTabelas;
	private JTree treeProjeto;
	private JTree treeViews;
	private JTree treeCamposView;
	private final static String DBINTERNO = "MercaradINT.sqlite";
	private final static String DBTEMP = "MercaradTEMP.sqlite";

	private JEditorPane codeEditorImportTables = null;
	private JEditorPane codeEditorSql = null;
	private JEditorPane codeEditorTables = null;
	private JEditorPane codeEditorVo = null;
	private JEditorPane codeEditorDA = null;
	private JEditorPane codeEditorOutput = null;

	private JTabbedPane tabbedPane;

	private List<ViewVO> views;
	private ViewVO selectedView;
	private CampoVO selectedViewCampo;

	private Utils utils = new Utils();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Principal window = new Principal();
					window.frmMercajava.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Principal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmMercajava = new JFrame();
		frmMercajava.setTitle("Mercanet");
		frmMercajava.setExtendedState(Frame.MAXIMIZED_BOTH);
		frmMercajava.setBounds(100, 100, 795, 581);
		frmMercajava.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		frmMercajava.getContentPane().add(panel, BorderLayout.NORTH);

		JButton btnLerScript = new JButton("Ler Tabelas");
		btnLerScript.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LerTabelas();
			}
		});

		JButton btnLerViews = new JButton("Ler Views");
		btnLerViews.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LerViews();
			}
		});
		panel.add(btnLerViews);
		panel.add(btnLerScript);

		JButton btnLerTelas = new JButton("Ler Telas");
		btnLerTelas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LerProjeto();
			}
		});
		panel.add(btnLerTelas);

		JSplitPane splitPane = new JSplitPane();
		splitPane.setDividerSize(4);
		splitPane.setMinimumSize(new Dimension(209, 250));
		frmMercajava.getContentPane().add(splitPane, BorderLayout.CENTER);

		rootNodeTabelas = new DefaultMutableTreeNode();
		rootNodeProjeto = new DefaultMutableTreeNode();
		rootNodeViews = new DefaultMutableTreeNode();
		rootNodeCamposView = new DefaultMutableTreeNode();

		JSplitPane spliPanelNavigation = new JSplitPane();
		spliPanelNavigation.setSize(new Dimension(250, 0));
		spliPanelNavigation.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setLeftComponent(spliPanelNavigation);

		JScrollPane scrollPane_1 = new JScrollPane();
		spliPanelNavigation.setLeftComponent(scrollPane_1);
		treeTabelas = new JTree(rootNodeTabelas);
		scrollPane_1.setViewportView(treeTabelas);

		treeProjeto = new JTree(rootNodeProjeto);
		spliPanelNavigation.setRightComponent(treeProjeto);
		treeTabelas.addMouseListener(onNodeSelectTabelas);
		spliPanelNavigation.setDividerLocation(300);

		JSplitPane splitPane_1 = new JSplitPane();
		splitPane.setRightComponent(splitPane_1);

		JSplitPane splitPane_2 = new JSplitPane();
		splitPane_2.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane_1.setLeftComponent(splitPane_2);

		treeViews = new JTree(rootNodeViews);
		treeViews.addMouseListener(onNodeSelectViews);

		splitPane_2.setLeftComponent(treeViews);

		treeCamposView = new JTree(rootNodeCamposView);
		treeCamposView.addMouseListener(onNodeSelectCamposView);
		splitPane_2.setRightComponent(treeCamposView);
		splitPane_2.setDividerLocation(300);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				geraOutput(false);
			}
		});
		splitPane_1.setRightComponent(tabbedPane);

		// IMPORT TABLES
		JPanel panelImportTables = new JPanel();
		tabbedPane.addTab("ImportTables", null, panelImportTables, null);

		DefaultSyntaxKit.initKit();

		// override an action and the popup menu
		// Configuration config =
		// DefaultSyntaxKit.getConfig(DefaultSyntaxKit.class);
		// config.put("Action.find", "my.custom.FindReplaceAction, menu F");
		// config.put("PopupMenu",
		// "cut-to-clipboard,copy-to-clipboard,paste-from-clipboard,-,select-all,-,undo,redo,-,find,find-next,goto-line,jump-to-pair,-,complete-word,-,toggle-comments");

		panelImportTables.setLayout(new BoxLayout(panelImportTables, BoxLayout.X_AXIS));

		codeEditorImportTables = new JEditorPane();

		JScrollPane scrPane = new JScrollPane(codeEditorImportTables);
		scrPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panelImportTables.add(scrPane);
		panelImportTables.doLayout();
		codeEditorImportTables.setContentType("text/java");
		codeEditorImportTables.setText(EditorManager.getTextImportTable());

		// SQL
		JPanel panelSql = new JPanel();
		tabbedPane.addTab("Sqlite", null, panelSql, null);

		panelSql.setLayout(new BoxLayout(panelSql, BoxLayout.X_AXIS));

		codeEditorSql = new JEditorPane();
		JScrollPane scrPanelSql = new JScrollPane(codeEditorSql);
		scrPanelSql.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrPanelSql.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panelSql.add(scrPanelSql);
		panelSql.doLayout();
		codeEditorSql.setContentType("text/sql");
		codeEditorSql.setText(EditorManager.getTextScriptSqLite());

		// SQL
		JPanel panelTables = new JPanel();
		tabbedPane.addTab("Tables", null, panelTables, null);
		// DefaultSyntaxKit.initKit();
		panelTables.setLayout(new BoxLayout(panelTables, BoxLayout.X_AXIS));

		codeEditorTables = new JEditorPane();
		JScrollPane scrPanelTables = new JScrollPane(codeEditorTables);
		scrPanelTables.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrPanelTables.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panelTables.add(scrPanelTables);
		panelTables.doLayout();
		codeEditorTables.setContentType("text/java");
		codeEditorTables.setText(EditorManager.getTextTables());
		splitPane_1.setDividerLocation(220);

		JPanel panelVo = new JPanel();
		tabbedPane.addTab("VO", null, panelVo, null);
		// DefaultSyntaxKit.initKit();
		panelVo.setLayout(new BoxLayout(panelVo, BoxLayout.X_AXIS));

		codeEditorVo = new JEditorPane();
		JScrollPane scrPanelVo = new JScrollPane(codeEditorVo);
		scrPanelVo.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrPanelVo.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panelVo.add(scrPanelVo);
		panelVo.doLayout();
		codeEditorVo.setContentType("text/java");
		//codeEditorVo.setText(EditorManager.getTextTables());
		splitPane_1.setDividerLocation(220);

		JPanel panelDA = new JPanel();
		tabbedPane.addTab("DataAcess", null, panelDA, null);
		// DefaultSyntaxKit.initKit();
		panelDA.setLayout(new BoxLayout(panelDA, BoxLayout.X_AXIS));

		codeEditorDA = new JEditorPane();
		JScrollPane scrPanelDA = new JScrollPane(codeEditorDA);
		scrPanelDA.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrPanelDA.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panelDA.add(scrPanelDA);
		panelDA.doLayout();
		codeEditorDA.setContentType("text/java");
		//codeEditorDA.setText(EditorManager.getTextTables());
		splitPane_1.setDividerLocation(220);

		splitPane.setDividerLocation(220);

		JPanel panel_1 = new JPanel();
		frmMercajava.getContentPane().add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		panel_1.setPreferredSize(new Dimension(100, 100));

		codeEditorOutput = new JEditorPane();
		JScrollPane scrollPane = new JScrollPane(codeEditorOutput);

		panel_1.add(scrollPane);

	}

	private MouseAdapter onNodeSelectTabelas = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			int selRow = treeTabelas.getRowForLocation(e.getX(), e.getY());
			TreePath selPath = treeTabelas.getPathForLocation(e.getX(), e.getY());
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
			if (selRow != -1) {
				if (e.getClickCount() == 1) {
					// System.out.println("1");
					// mySingleClick(selRow, selPath);
				} else if (e.getClickCount() == 2) {
					DialogDescTable frmDesc = new DialogDescTable(managerLite, (String) node.getUserObject());
					frmDesc.setVisible(true);
				}
			}
		}
	};

	private MouseAdapter onNodeSelectTelas = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			int selRow = treeTabelas.getRowForLocation(e.getX(), e.getY());
			TreePath selPath = treeTabelas.getPathForLocation(e.getX(), e.getY());
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
			if (selRow != -1) {
				if (e.getClickCount() == 1) {
					// System.out.println("1");
					// mySingleClick(selRow, selPath);
				} else if (e.getClickCount() == 2) {
					DialogDescTable frmDesc = new DialogDescTable(managerLite, (String) node.getUserObject());
					frmDesc.setVisible(true);
				}
			}
		}
	};

	private MouseAdapter onNodeSelectViews = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			int selRow = treeViews.getRowForLocation(e.getX(), e.getY());
			TreePath selPath = treeViews.getPathForLocation(e.getX(), e.getY());
			// DefaultMutableTreeNode node = (DefaultMutableTreeNode)
			// selPath.getLastPathComponent();
			if (selRow != -1) {
				// if (e.getClickCount() == 1) {
				// System.out.println("1");
				// mySingleClick(selRow, selPath);
				// } else
				if (e.getClickCount() == 1) {
					selectedView = views.get(treeViews.getSelectionRows()[0]-1);
					managerOracle.getCamposViewAndroidNovo(selectedView);
					geraOutput(false);

					setTreeCamposView(selectedView);

					// final WordSearcher searcher = new WordSearche(textPane);
					//
					// tf.addActionListener(new ActionListener() {
					// public void actionPerformed(ActionEvent evt) {
					// word = tf.getText().trim();
					// int offset = searcher.search(word);
					// if (offset != -1) {
					// try {
					// textPane.scrollRectToVisible(textPane
					// .modelToView(offset));
					// } catch (BadLocationException e) {
					// }
					// }
					// }
					// });

					// int offset =
					// codeEditorImportTables.getDocument().getText().indexof(selectedView.getTabelaSqlite());
					// codeEditorImportTables.scrollRectToVisible(codeEditorImportTables.viewToModel(offset));
					// codeEditorImportTables.l
					// .
					// DialogDescTable frmDesc = new
					// DialogDescTable(managerLite, (String)
					// node.getUserObject());
					// frmDesc.setVisible(true);
				}
			}
		}

	};

	private MouseAdapter onNodeSelectCamposView = new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			if (selectedView == null)
				return;
			int selRow = treeCamposView.getRowForLocation(e.getX(), e.getY());
			// int selRowView = treeViews.getRowForLocation(e.getX(), e.getY());
			TreePath selPath = treeCamposView.getPathForLocation(e.getX(), e.getY());
			// DefaultMutableTreeNode node = (DefaultMutableTreeNode)
			// selPath.getLastPathComponent();
			// ViewVO view = views.get(selRowView - 1);
			if (selRow != -1) {
				// if (e.getClickCount() == 1) {
				// System.out.println("1");
				// mySingleClick(selRow, selPath);
				// } else
				if (e.getClickCount() == 1) {
					selectedViewCampo = selectedView.getCampos().get(selRow - 1);
					geraOutput(true);
					// CampoVO campo = selectedView.getCampos().get(selRow - 1);
					// managerOracle.getCamposViewAndroid(selectedView);

					// final WordSearcher searcher = new WordSearche(textPane);
					//
					// tf.addActionListener(new ActionListener() {
					// public void actionPerformed(ActionEvent evt) {
					// word = tf.getText().trim();
					// int offset = searcher.search(word);
					// if (offset != -1) {
					// try {
					// textPane.scrollRectToVisible(textPane
					// .modelToView(offset));
					// } catch (BadLocationException e) {
					// }
					// }
					// }
					// });

					// int offset =
					// codeEditorImportTables.getDocument().getText().indexof(selectedView.getTabelaSqlite());
					// codeEditorImportTables.scrollRectToVisible(codeEditorImportTables.viewToModel(offset));
					// codeEditorImportTables.l
					// .
					// DialogDescTable frmDesc = new
					// DialogDescTable(managerLite, (String)
					// node.getUserObject());
					// frmDesc.setVisible(true);
				}
			}
		}

	};

	private void geraOutput(boolean campoSelect) {
		int currentab = tabbedPane.getSelectedIndex();
		if (codeEditorOutput == null)
			return;
		codeEditorOutput.setText("");
		switch (currentab) {
		case 0:// import tables
			if (selectedView == null)
				return;
			if (!highlight(codeEditorImportTables, selectedView.getPatterImportTable())) {

			}
			codeEditorOutput.setText(selectedView.getStringImportTable());
			codeEditorOutput.setContentType("text/java");
			utils.copyStringToClipboard(selectedView.getStringImportTable());
			break;
		case 1:// sqlite
			if (campoSelect) {
				// int[] selecionado = treeCamposView.getSelectionRows();
				// // int selRow = treeCamposView.getRowForLocation(e.getX(),
				// e.getY());
				CampoVO campo = selectedViewCampo;
				if (!highlight(codeEditorSql, campo.getPatterSqLiteNaoCompleto())) {

				}
				codeEditorOutput.setText(campo.getStringSqlite());
				codeEditorOutput.setContentType("text/sql");
				utils.copyStringToClipboard(campo.getStringSqlite());

			} else {
				if (!highlight(codeEditorSql, selectedView.getPatterSqLiteNaoCompleto())) {

				}
				codeEditorOutput.setText(selectedView.getStringSqLiteCompleto());
				codeEditorOutput.setContentType("text/sql");
				utils.copyStringToClipboard(selectedView.getStringSqLiteCompleto());
			}
			break;
		case 2:// tables
			if (campoSelect) {
				CampoVO campo = selectedViewCampo;
				if (!highlight(codeEditorTables, campo.getPatternTables())) {

				}
				codeEditorOutput.setText(campo.getPatternTables());
				codeEditorOutput.setContentType("text/java");
				utils.copyStringToClipboard(campo.getPatternTables());

			} else {
				if (!highlight(codeEditorTables, selectedView.getPatternTables())) {
					

				}
				codeEditorOutput.setText(selectedView.getPatternTables());
				codeEditorOutput.setContentType("text/java");
				utils.copyStringToClipboard(selectedView.getStringTables());

			}
			break;

		case 3:// VO
//			if (campoSelect) {
//				CampoVO campo = selectedViewCampo;
//				
////				if (!highlight(codeEditorVo,campo.getStringSqlite())) {
////
////				}
//				codeEditorOutput.setText(campo.getStringVo());
//				codeEditorOutput.setContentType("text/java");
//				utils.copyStringToClipboard(campo.getStringVo());
//
//			} else {
//				if (!highlight(codeEditorVo, selectedView.getStringVoCompleto())) {
//					
//				}
			codeEditorVo.setText("");
				List<String> listArquivos = new ArrayList<String>();
				listArquivos = utils.findTextInFile(utils.getDirbase(),listArquivos,true,selectedView.getTabelaSqlite(),".java"); 
				if (listArquivos.size()==1){
					codeEditorVo.setText(utils.openFile(listArquivos.get(0)));
				}
				codeEditorOutput.setContentType("text/java");
				//utils.copyStringToClipboard(selectedView.getStringVoCompleto());
//			}
			break;
			
			
		case 4:// DA
//			if (campoSelect) {
//				CampoVO campo = selectedViewCampo;
////				if (!highlight(codeEditorVo,campo.getStringSqlite())) {
////
////				}
//				codeEditorOutput.setText(campo.getStringDA(selectedView.getNome()));
//				codeEditorOutput.setContentType("text/java");
//				utils.copyStringToClipboard(campo.getStringDA(selectedView.getNome()));
//
//			} else {
////				if (!highlight(codeEditorVo, selectedView.getStringDACompleto())) {
////					
////				}
//				codeEditorOutput.setText(selectedView.getStringDACompleto());
//				codeEditorOutput.setContentType("text/java");
//				utils.copyStringToClipboard(selectedView.getStringDACompleto());
//
//			}
			
			codeEditorDA.setText("");
			List<String> listArquivos2= new ArrayList<String>();
			listArquivos2 = utils.findTextInFile(utils.getDirbase(),listArquivos2,true,selectedView.getTabelaSqlite(),"dataacess.java"); 
			if (listArquivos2.size()==1){
				codeEditorDA.setText(utils.openFile(listArquivos2.get(0)));
			}
			codeEditorOutput.setContentType("text/java");
			break;



		default:
			break;
		}
	}

	Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(
			UIManager.getColor("Table.selectionBackground"));

	// A private subclass of the default highlight painter
	class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
		public MyHighlightPainter(Color color) {
			super(color);
		}
	}

	// Creates highlights around all occurrences of pattern in textComp
	private boolean highlight(JEditorPane textComp, String pattern) {
		// First remove all old highlights
		removeHighlights(textComp);

		Highlighter hilite = textComp.getHighlighter();
		Document doc = textComp.getDocument();
		String text = null;
		try {
			text = doc.getText(0, doc.getLength());
		} catch (BadLocationException ex) {
			ex.printStackTrace();
			// Logger.getLogger(WordEditorSearch.class.getName()).log(Level.SEVERE,
			// null, ex);
		}
		int pos = 0;

		// Search for pattern
		if ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) == -1) {
			return false;
		}

		while ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) >= 0) {
			try {
				// Create highlighter using private painter and apply around
				// pattern
				hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter);
			} catch (BadLocationException ex) {
				ex.printStackTrace();
				// Logger.getLogger(WordEditorSearch.class.getName()).log(Level.SEVERE,
				// null, ex);
			}
			// textComp.scrollToReference(pattern);
			try {
				textComp.scrollRectToVisible(textComp.modelToView(pos));
			} catch (Exception e) {
				e.printStackTrace();
			}
			// textComp.scrollRectToVisible(textComp.getVisibleRect());
			pos += pattern.length();
		}
		return true;
	}

	// Removes only our private highlights
	private void removeHighlights(JEditorPane textComp) {
		Highlighter hilite = textComp.getHighlighter();
		Highlighter.Highlight[] hilites = hilite.getHighlights();

		for (int i = 0; i < hilites.length; i++) {
			if (hilites[i].getPainter() instanceof MyHighlightPainter) {
				hilite.removeHighlight(hilites[i]);
			}
		}
	}

	private void LerTabelas() {
		File file = new File(DBTEMP);
		if (file.exists())
			file.delete();

		managerLite = new DbManagerSqLite(DBTEMP);
		managerLite.executaScriptCreate(utils.getDirbase() + "assets\\scripts\\version1.sql");
		setTreeTabelas();

	}

	private void LerProjeto() {
		Projeto p = new Projeto(utils.getDirbase());
		ProjetoVO projetos = p.getRootProject();

		rootNodeProjeto.removeAllChildren();
		rootNodeProjeto.setUserObject("Telas");
		setTreeProjeto(projetos, rootNodeProjeto, true);

		((DefaultTreeModel) treeProjeto.getModel()).reload();
	}

	private void LerViews() {
		managerOracle = new DbManagerOracle();
		views = managerOracle.getViewsAndroid();
		setTreeViews();

	}

	private void setTreeTabelas() {
		rootNodeTabelas.removeAllChildren();
		rootNodeTabelas.setUserObject("Tabelas");
		List<String> tabelas = managerLite.getAllTables();
		for (String nome : tabelas) {
			rootNodeTabelas.add(new DefaultMutableTreeNode(nome));
		}
		((DefaultTreeModel) treeTabelas.getModel()).reload();
	}

	private void setTreeProjeto(ProjetoVO root, DefaultMutableTreeNode parent, Boolean recursive) {
		if (root.getChilds() == null)
			return;
		for (int i = 0; i < root.getChilds().size(); i++) { // loop through each
			ProjetoVO pp = root.getChilds().get(i);
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(pp.getNomeTela());
			if (pp.getTipo() == 1 && recursive) {
				parent.add(node); // add as a child node
				setTreeProjeto(pp, node, recursive); // call again for the
														// subdirectory
			} else if (pp.getTipo() == 0) { // otherwise, if it isn't a
											// directory
				parent.add(node); // add it as a node and do nothing else
			}
		}
	}

	private void setTreeViews() {
		rootNodeViews.removeAllChildren();
		rootNodeViews.setUserObject("Views");
		for (ViewVO view : views) {
			rootNodeViews.add(new DefaultMutableTreeNode(view.getNome()));
		}
		((DefaultTreeModel) treeViews.getModel()).reload();

	}

	private void setTreeCamposView(ViewVO view) {
		rootNodeCamposView.removeAllChildren();
		rootNodeCamposView.setUserObject("Campos");
		for (CampoVO campo : view.getCampos()) {
			rootNodeCamposView.add(new DefaultMutableTreeNode(campo.getNome()));
		}
		((DefaultTreeModel) treeCamposView.getModel()).reload();
	}
}
